package IPADistribution.PlistUtil;

import IPADistribution.Constant.Constant;
import IPADistribution.Servlets.Upload;
import org.apache.commons.fileupload.FileItem;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PlistUtil {
    public static String ipaUrl = "ipaUrl";
    public static String bundleIdentifer = "bundleIdentifer";
    public static String bundleVersion = "bundleVersion";
    public static String tips = "tips";

    static public void replacePlistContent(File templatePlist , File destPlist, Map<String, String>replaceMap) throws IOException {
        FileReader reader = new FileReader(templatePlist);
        String fileContent;
        StringBuffer fileData = new StringBuffer();
        char[] buf = new char[1024];
        int numRead=0;
        while((numRead=reader.read(buf)) != -1){
            String readData = String.valueOf(buf, 0, numRead);
            fileData.append(readData);
        }
        fileContent = fileData.toString();

        //内容替换
        for (Map.Entry<String, String> entry : replaceMap.entrySet()) {
            fileContent = fileContent.replaceAll(entry.getKey(), entry.getValue());
        }

        FileWriter writer = new FileWriter(destPlist);
        writer.write(fileContent);
        writer.flush();
        writer.close();
    }

    static public void generatePlist(List<FileItem> items, ServletContext context, String fileUrl) throws ServletException, IOException, UnsupportedEncodingException {
        String env = null;
        String ipaName = null;
        String bundleIdentifer = null;
        String bundleVersion = null;

        for (FileItem item : items) {
            String field = item.getFieldName();
            System.out.println("field:"+field);
            if (field.equals(Upload.env_key)) {
                env = item.getString("UTF-8");
            }else if (field.equals(Upload.ipaName_key)){
                ipaName = item.getString("UTF-8");
            }else if (field.equals(Upload.bundleIdentifer_key)){
                bundleIdentifer = item.getString("UTF-8");
            }else if (field.equals(Upload.bundleVersion_key)){
                bundleVersion = item.getString("UTF-8");
            }
        }

        File templatePlist = new File(context.getRealPath(File.separator+"WEB-INF"+ File.separator+"template.plist"));
        if (!templatePlist.exists()) throw new ServletException("templatePlist not found in WEB-INF!");

        Date now = new Date();
        String date = new SimpleDateFormat("yyyy-MM-dd").format(now);

        String destPlistDir = Constant.plist_path+File.separator+bundleVersion+File.separator+env.toUpperCase()+File.separator+date;
        String plistName = ipaName+"_"+bundleVersion+"_"+env+"_"+String.valueOf(now.getTime())+".plist";
        String realDestPlistDir = context.getRealPath(destPlistDir);

        File destPlistDirector = new File(realDestPlistDir);
        if (!destPlistDirector.exists()){
            destPlistDirector.mkdirs();
        }

        File destPlist = new File(context.getRealPath(destPlistDir+File.separator+plistName));
        if (!destPlist.exists()){
            destPlist.createNewFile();
        }

        HashMap<String, String> replaceMap = new HashMap<>();
        //replaceMap.put(env_key,env);
        replaceMap.put(PlistUtil.ipaUrl,fileUrl);
        replaceMap.put(PlistUtil.bundleIdentifer,bundleIdentifer);
        replaceMap.put(PlistUtil.bundleVersion,bundleVersion);
        replaceMap.put(PlistUtil.tips,ipaName);
        PlistUtil.replacePlistContent(templatePlist, destPlist, replaceMap);
    }
}
